/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package ecarx.xsf.notificationcenter.manager;

import android.util.Log;

import ecarx.xsf.notificationcenter.model.Competition;

import ecarx.xsf.notificationcenter.common.Constants;
import ecarx.xsf.notificationcenter.scenecard.ISceneCardInfoV2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * FileName: QueueManager.java <br>
 * Description : 队列管理 <br>
 * Create Time: 2020/03/18 <br>
 * Update Time: 2021/8/4 11:32 <br>
 *
 * @since 1.0.0
 */
public final class QueueManager {

    private static final String TAG = "QueueManager";

    /**
     * 对列
     */
    private final List<Competition> resultList = new ArrayList<>();

    private final List<ISceneCardInfoV2> sceneCardInfoList = new ArrayList<>();

    /**
     * 消息竞合完成回掉
     */
    private IAddQueueCompleteListener listener;

    public synchronized void addQueue(Competition competition) {
        Log.i(TAG, "card queue size " + resultList.size());
        if (resultList.size() >= Constants.MAX_QUEUE_SIZE) {
            Log.w(TAG, "queue larger than 20.");
            return;
        }

        resultList.add(competition);
        Collections.sort(resultList);
        if (listener != null) {
            listener.onComplete();
        }
    }


    public Competition getCompetition() {
        int size = resultList.size();
        if (size > 0) {
            return resultList.get(size - 1);
        }

        return null;
    }


    public void removeCompetitionFromQueue(Competition competition) {
        resultList.remove(competition);
    }

    public String getRemovedInfoKey(String key) {
        Competition competition = null;
        for (Competition cpt : resultList) {
            if (cpt.getNotificationBar().getKey().equals(key)) {
                competition = cpt;
            }
        }
        if (competition != null) {
            return competition.getNotificationBar().getKey();
        }
        return "";
    }

    public void removeCompetitionByKey(String key) {
        Log.i(TAG, "remove competition key " + key);
        Competition competition = null;
        for (Competition cpt : resultList) {
            if (cpt.getNotificationBar().getKey().equals(key)) {
                competition = cpt;
            }
        }

        if (competition != null) {
            Log.i(TAG, "removed competition key " + key);
            resultList.remove(competition);
        }
    }

    public void addSceneCardQueue(ISceneCardInfoV2 iSceneCardInfo) {
        Log.i(TAG, "scene card queue size > " + sceneCardInfoList.size());
        if (sceneCardInfoList.size() >= Constants.MAX_QUEUE_SIZE) {
            Log.w(TAG, "scene card queue larger than 20.");
            return;
        }
        ISceneCardInfoV2 info = null;
        for (ISceneCardInfoV2 cardInfo : sceneCardInfoList) {
            if (cardInfo.getKey().equals(iSceneCardInfo.getKey())) {
                info = cardInfo;
            }
        }
        if (info != null) {
            sceneCardInfoList.remove(info);
        }
        sceneCardInfoList.add(iSceneCardInfo);
        Collections.sort(sceneCardInfoList);
        if (listener != null) {
            listener.onSceneCardComplete();
        }
    }

    public void removeSceneCard(ISceneCardInfoV2 iSceneCardInfo) {
        Log.i(TAG, "remove key = " + iSceneCardInfo.getKey() + " from queue ");
        ISceneCardInfoV2 info = null;
        for (ISceneCardInfoV2 cardInfo : sceneCardInfoList) {
            if (cardInfo.getKey().equals(iSceneCardInfo.getKey())) {
                info = cardInfo;
            }
        }
        if (info != null) {
            sceneCardInfoList.remove(info);
        }
    }

    public ISceneCardInfoV2 getSceneCard() {
        int size = sceneCardInfoList.size();
        if (size > 0) {
            return sceneCardInfoList.get(size - 1);
        }
        return null;
    }


    public void setOnAddCompleteListener(IAddQueueCompleteListener listener) {
        this.listener = listener;
    }

    public interface IAddQueueCompleteListener {
        /**
         * 是否添加队列完成
         */
        void onComplete();

        /**
         * 场景卡片队列添加完成
         */
        void onSceneCardComplete();
    }


}
